local skel = fk.CreateSkill {
  name = "efengqi__zhiheng",
}

Fk:loadTranslationTable{
  ["efengqi__zhiheng"] = "制衡",
  [":efengqi__zhiheng"] = "出牌阶段各限一次，你可以：令一名手牌数为奇数的角色摸三张牌并弃置半数手牌；令一名手牌数为偶数的角色弃置半数手牌并摸三张牌。",

  ["#efengqi__zhiheng"] = "制衡：令一名角色摸三弃半，或弃半摸三！",
  ["efengqi__zhiheng1"] = "令奇数手牌数角色摸三弃半",
  ["efengqi__zhiheng2"] = "令偶数手牌数角色弃半摸三",

  ["$efengqi__zhiheng1"] = "不急不躁，稳谋应对。",
  ["$efengqi__zhiheng2"] = "制衡互牵，大局可安。",
}

skel:addEffect("active", {
  anim_type = "drawcard",
  prompt = "#efengqi__zhiheng",
  card_num = 0,
  target_num = 1,
  interaction = function (self, player)
    local all_choices = {"efengqi__zhiheng1", "efengqi__zhiheng2"}
    local choices = table.filter(all_choices, function (ch)
      return player:getMark(ch.."-phase") == 0
    end)
    if #choices > 0 then
      return UI.ComboBox { choices = choices, all_choices = all_choices }
    end
  end,
  times = function (self, player)
    return 2 - player:getMark("efengqi__zhiheng1-phase") - player:getMark("efengqi__zhiheng2-phase")
  end,
  can_use = function(self, player)
    return player:getMark("efengqi__zhiheng1-phase") == 0 or player:getMark("efengqi__zhiheng2-phase") == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to, selected)
    if #selected > 0 then return false end
    if self.interaction.data == "efengqi__zhiheng1" then
      return to:getHandcardNum() % 2 == 1
    else
      return to:getHandcardNum() % 2 == 0
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local to = effect.tos[1]
    local choice = self.interaction.data
    room:setPlayerMark(player, choice.."-phase", 1)
    local num = 3
    if choice:endsWith("1") then
      to:drawCards(num, skel.name)
      if not to.dead and not to:isKongcheng() then
        num = to:getHandcardNum() // 2
        room:askToDiscard(to, {min_num = num, max_num = num, include_equip = false, skill_name = skel.name, cancelable = false})
      end
    else
      num = to:getHandcardNum() // 2
      room:askToDiscard(to, {min_num = num, max_num = num, include_equip = false, skill_name = skel.name, cancelable = false})
      if not to.dead then
        to:drawCards(3, skel.name)
      end
    end
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "efengqi__zhiheng1-phase", 0)
  player.room:setPlayerMark(player, "efengqi__zhiheng2-phase", 0)
end)

return skel
